package com.dogcatfishdish.depthdisplay 
{
	import com.greensock.TweenLite;
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	/**
	 * ...
	 * @author bb
	 */
	public class AdvancedDragItem extends DragItem
	{
		public var rollZ:Number = 5;
		protected var isOver:Boolean = false;
		protected var tween:TweenLite;
		
		public function AdvancedDragItem(item:DisplayObject) 
		{
			super(item);

		}
		
		override protected function addListeners():void {
			super.addListeners();
			
			addEventListener(MouseEvent.ROLL_OVER, onRollOver);
			addEventListener(MouseEvent.ROLL_OUT, onRollOut);
		}
		
		private function onRollOut(e:MouseEvent):void 
		{	
			isOver = false;
				
			var o:Object = { };
			drop(o);
		}
		
		private function onRollOver(e:MouseEvent):void 
		{
			if (isDragging)
				return;
				
			isOver = true;
				
			var o:Object = { };
			o.z = rollZ;
			tween = TweenLite.to(this, upDuration, o );
		}
		
		override protected function drop(o:Object):void {
				
			if (isDragging)
				return;

			o.z = zMin;
			
			o.ease = downEase;
			o.onComplete = function():void { isDragging = false; };

			tween = TweenLite.to(this, downDuration, o);
		}
		
		override protected function miss():void {
			var o:Object = { };
			//if (returnToBase) {
			//	o.x = initialPosition.x;
			//	o.y = initialPosition.y;
			//}
			drop(o);
		}
		
		override protected function hit():void {
			var o:Object = { };
			if (snapToTarget) {
				var pt:Point = container.globalToLocal(target.parent.localToGlobal(new Point(target.x, target.y)));
				o.x = pt.x;
				o.y = pt.y;
			}
			drop(o);
		}
		
		override protected function onMouseDown(e:MouseEvent):void {
			super.onMouseDown(e);
			addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
		}
		
		override protected function onMouseUp(e:MouseEvent):void {
			super.onMouseUp(e);
			removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			clearAllTargets();
		}
		
		protected function clearAllTargets():void {
			for each(var t:BaseTargetItem in container.targets) {
				t.onDragItemOut();
			}
		}
		
		protected function onMouseMove(e:MouseEvent):void 
		{
			for each(var t:BaseTargetItem in container.targets) {
				if (hitTestObject(t)) {
					t.onDragItemOver();
				} else {
					t.onDragItemOut();
				}
			}
		}
		
	}

}